<template>
	<view class="msgbody">
		
		<view class="footer">
			<button class="btnline" @click="submit" v-if="msgdata.advisoryStatus == null">预约</button>
			<block v-else>
				<view class="btn3 time" v-if="msgdata.consultTime && !canLine" style="">
					<uni-countdown :font-size="14" :day="time" :hour="si" :minute="fen" :second="miao" @timeup="timeup" color="#FFFFFF" splitorColor='#FFFFFF'/> 后开始咨询
				</view>
				<button class="btn3 time" v-else-if="canLine" @click="callUser">点击连线</button>
				<block v-else>
					<button class="btn1" @click="submit">取消</button>
					<button class="btn1" @click="queryQuestion">填写问卷</button>
					<button class="btn2" v-if='msgdata.consultTime == null'>预约中</button>
				</block>
			</block>
		</view>
		<view class="main">
			<view class="">
				<text>咨询消耗:</text>
				<image :src="tb" mode="" style="width: 40rpx;height: 40rpx;vertical-align: middle"></image>
				{{msgdata.expertToll == null ? 0:msgdata.expertToll}}/小时
			</view>
			<view class="">
				<text>在线时间:</text> {{msgdata.expertOnline == null ? '暂无':msgdata.expertOnline}}
			</view>
			<view class="">
				<text>擅长领域:</text>
				<uni-tag :text="item" v-for="item in expertExcels"
					custom-style="background-color: #FFF0E5; border-color: #FFF0E5; color: #FF7F22;margin-left: 15rpx;">
				</uni-tag>
				<!-- <uni-tag text="情绪压力"
					custom-style="background-color: #FFF0E5; border-color: #FFF0E5; color: #FF7F22;margin-left: 15rpx;">
				</uni-tag> -->
			</view>
			<!-- <view class="zhpj">综合评价</view> -->
		</view>
<!-- 		<view class="pj">
			<view class="box">
				<view class="count">
					{{msgdata.comprehensiveScore}}
				</view>
				<view class="pf">
					<uni-rate :readonly="true" :value="msgdata.comprehensiveScore" activeColor="#FF8B25" />
					<view class="zhpf">
						综合评分
					</view>
				</view>
				<view class="borders">

				</view>
				<view class="pl">
					<view class="nine">
						{{msgdata.goodRating}}
					</view>
					<view class="hpl">
						好评率
					</view>
				</view>
			</view>
		</view>
		<view class="content" v-for="item in list">
			<view class="con_txt">
				<view class="con_box">
					<image :src="img" mode="" style="width: 100rpx;height: 100rpx;border-radius: 50%;"></image>
					<view class="xing">
						<view class="">
							{{item.createBy}}
						</view>
						<view class="">
							<uni-rate :readonly="true" :value="item.grade" activeColor="#FF8B25" />
						</view>
					</view>
				</view>
				<view class="con_time">
					{{item.createTime}}
				</view>
			</view>
			<view class="con_ft">
				{{item.vocabulary}}
			</view>
		</view> -->
		<view class="">
			<!-- 提示信息弹窗 -->
			<uni-popup ref="refDialog" type="dialog" >
				<uni-popup-dialog :type="msgType" cancelText="关闭" confirmText="确定" title="提示" content="确定要取消预约吗？" @confirm="dialogConfirm"></uni-popup-dialog>
			</uni-popup>
		</view>
		
		<view>
			<!-- 提示窗示例 -->
			<uni-popup ref="alertDialog" type="dialog">
				<view class="uni_dia">
					<view class="uni_sucess">
						预约失败
					</view>
					<view class="uni_font" style="width: 490rpx;margin-top: 60rpx;line-height: 40rpx;">
						您存在未支付订单，请先支付
					</view>
					<button class="uni_btn" @click="order">去支付</button>
				</view>
		
				<!-- </uni-popup-dialog> -->
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		imgBaseUrl
	} from '@/utils/baseUrl.js'
	import dayjs from 'dayjs';
	import {busConsultdel,checkConsultOnline,consultOnline,checkPendingOrder} from '@/utils/OnetoOneaApi.js'
	export default {
		props: {
			msgdata: {
				require: true
			}
		},
		data() {
			return {
				tb: imgBaseUrl + '/img/wshing/tab.png',
				img: 'https://img0.baidu.com/it/u=56109659,3345510515&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1697648400&t=22ca95e4e119cd4ef23fcd9b54fc6b10',
				list: [],
				expertExcels: [],
				msgType: 'warn',
				type: 'center',
				time:'',
				si:'',
				fen:'',
				miao:'',
				canLine: false,
				orderId: '',
				userOpenId: '',//专家的指派咨询openid
				userId: ''//专家id
			}
		},
		mounted() {
			let a = this.msgdata.consultTime
			let c =dayjs(a).diff(new Date,'second')
			this.secondsFormat(c);
			
			this.userOpenId = this.msgdata.openid;
			this.userId = this.msgdata.expertId;
			if(this.msgdata.expertExcel) {
				this.expertExcels = this.msgdata.expertExcel.split(' ');
			}
			
			// todo.. 测试登录，后续删除
			// var userId = uni.getStorageSync("userId");
			// consultOnline(userId)
			// console.log(userId, '本人online')
		},
		methods: {
			queryQuestion(){
				let link = this.$parent.link
				uni.navigateTo({
					url: `../../../subPackagesB/pages/WebView/WebView?url=${link}`
				});
			},
			secondsFormat( s ) { 
				console.log(s,'时间差')
			    var day = Math.floor( s/ (24*3600) ); // Math.floor()向下取整 
			    var hour = Math.floor( (s - day*24*3600) / 3600); 
			    var minute = Math.floor( (s - day*24*3600 - hour*3600) /60 ); 
			    var second = s - day*24*3600 - hour*3600 - minute*60; 
			    let days =  day + "天"  + hour + "时" + minute + "分" + second + "秒"; 
				this.time = day
				this.si = hour
				this.fen = minute
				this.miao = second
				
				this.canLine = (s < 0);
			},
			timeup(){
				this.canLine = true;
			},
			order(){
				var id = this.orderId;
				uni.navigateTo({
					url:`../../../subPackagesB/pages/MyOrder/orderDetail?Id=${id}`
				})
			},
			async callUser(){
				var listenOpenId = this.userOpenId;
				var openid = uni.getStorageSync("openid");
				wx.setEnable1v1Chat({
				  enable: true,
				  success: (res) => {
					wx.join1v1Chat({
					  caller: {
					    nickname: '我',
					    openid: openid,
					  },
					  listener: {
					    nickname: '专家',
					    openid: listenOpenId,
					  },
					})
				  },
				  fail: (err) => {
				  }
				})
				// if(!this.userOpenId) {
				// 	uni.showToast({
				// 		title: '专家不在线',
				// 		//将值设置为 success 或者直接不用写icon这个参数
				// 		icon: 'error',
				// 		//显示持续时间为 2秒
				// 		duration: 2000
				// 	})
				// 	return
				// }
				// const res = await checkConsultOnline(this.userId);
				// if(res.code == 200)
				// {
				// 			console.log('caller')
				// 		wx.setEnable1v1Chat({
				// 		  enable: true,
				// 		  success: (res) => {
				// 			wx.join1v1Chat({
				// 			  caller: {
				// 			    nickname: '我',
				// 			    openid: openid,
				// 			  },
				// 			  listener: {
				// 			    nickname: '专家',
				// 			    openid: listenOpenId,
				// 			  },
				// 			})
				// 		  },
				// 		  fail: (err) => {
				// 		  }
				// 		})
						
				// 	if(res.result.isOnline) {
				// 	} else {
				// 		uni.showToast({
				// 			title: '专家不在线',
				// 			//将值设置为 success 或者直接不用写icon这个参数
				// 			icon: 'error',
				// 			//显示持续时间为 2秒
				// 			duration: 2000
				// 		})
				// 	}
				// } else {
				// 	uni.showToast({
				// 		title: '专家不在线',
				// 		//将值设置为 success 或者直接不用写icon这个参数
				// 		icon: 'error',
				// 		//显示持续时间为 2秒
				// 		duration: 2000
				// 	})
				// }
			},
			async submit() {
				if (this.msgdata.advisoryStatus == null) {
					const checkRes = await checkPendingOrder();
					if (checkRes.result.isPendingOrder){
						this.orderId = checkRes.result.id
						this.$refs.alertDialog.open()
					}else{
						let id = this.$parent.id
						let link = this.$parent.link
						uni.navigateTo({
							url: `../../pages/make/make?id=${id}&link=${link}`
						})
					}
				} else if (this.msgdata.advisoryStatus == 0 || this.msgdata.advisoryStatus == 1) {
					console.log(this,'this');
					this.msgType = 'warn'
					this.$refs.refDialog.open()
				}
			},
			// 确定
		 async	dialogConfirm(){
			 const id = this.$parent.obj.consultId
			 
			 // console.log(this.$parent.queylist(this.$parent.id),'this');
				const res = await busConsultdel(id)
				if(res.code == 200)
				{
					this.$parent.queylist(this.$parent.id)
					uni.showToast({
						title: '取消预约成功!',
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'success',
						//显示持续时间为 2秒
						duration: 2000
					})
				}else
				{
					uni.showToast({
						title: '该状态不可取消预约',
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'error',
						//显示持续时间为 2秒
						duration: 2000
					})
				}
			}
		}
	}
</script>

<style scoped lang="less">
	.msgbody {
		padding: 37rpx;

		.main {
			line-height: 84rpx;
			font-size: 16px;
		}

		.zhpj {
			font-size: 16px;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
		}

		.pj {
			width: 100%;
			height: 100px;
			background: linear-gradient(90deg, #FFFEFA 0%, #FDFBEE 100%);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			opacity: 1;
			display: flex;
			justify-content: center;
			align-items: center;

			.box {
				// border: 1px solid red;
				width: 80%;
				height: 160rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.count {
					font-size: 100rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #FF8B25;
				}

				.borders {
					width: 1px;
					height: 31px;
					background: #D9D9D9;
					border-radius: 0px 0px 0px 0px;
					opacity: 1;
				}

				.pf {
					line-height: 59rpx;

					.zhpf {
						font-size: 12px;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}

				.pl {
					line-height: 53rpx;

					.nine {
						font-size: 24px;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #333333;
					}

					.hpl {
						font-size: 12px;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}
		}

		.content {
			margin-top: 20rpx;
			width: 100%;
			height: 300rpx;
			border-bottom: 1px solid #EAEAEA;

			.con_txt {
				display: flex;
				justify-content: space-between;

				.con_box {
					display: flex;

					.xing {
						margin-left: 15rpx;
					}
				}
				.con_time{
					font-size: 26rpx;
					font-weight: 400;
					color: #999999;
				}
			}

			.con_ft {
				line-height: 50rpx;
				margin-top: 20rpx;
			}
		}

		.footer {
			display: flex;
			
			.btnline {
				width: 670rpx;
				height: 88rpx;
				border-radius:  8rpx 8rpx 8rpx 8rpx;
				opacity: 1;
				border: 2rpx solid #FF7F22;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FF7F22;
				background-color: white;
			}

			.btn1 {
				width: 224rpx;
				height: 88rpx;
				line-height: 88rpx;
				border-radius:  8rpx 8rpx 8rpx 8rpx;
				opacity: 1;
				border: 2rpx solid #FF7F22;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FF7F22;
				background-color: white;
			}

			.btn2 {
				// width: 446rpx;
				height: 88rpx;
				line-height: 88rpx;
				background: #FF7F22;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				opacity: 1;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
			}

			.btn3 {
				width: 100%;
				height: 88rpx;
				background: #FF7F22;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				opacity: 1;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
			}
			.time{
				    display: flex;
				    align-items: center;
				    justify-content: center;
			}
		}
		.uni_dia {
			display: flex;
			flex-direction: column;
			align-items: center;
			text-align: center;
			background-color: white;
			border-radius: 20rpx;
			padding: 20px;
		
			.uni_sucess {
				font-size: 40rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;
				line-height: 32rpx;
			}
		
			.uni_font {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #666666;
				line-height: 36rpx;
			}
		
			.uni_btn {
				width: 320rpx;
				height: 80rpx;
				background: #FF7F22;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				color: #FFFFFF;
				margin-top: 68rpx;
			}
		}
	}
</style>